import {
  topCategory
} from 'api/constants.js'
import {
  findAllCatagory
} from 'api/catagory.js'

export default {
  namespaced: true,
  state() {
    return {
      // 分类商品，依赖constants.js
      catagory: topCategory.map(item => ({
        name: item
      }))
    }
  },
  //一旦获取到数据，则修改
  mutations: {
    setCatagory(state, payload) {
      state.catagory = payload
    },
    //二级组件的隐藏
    show(state, payload) {
      const currentCategory = state.catagory.find(item => item.id === payload)
      currentCategory.open = true
    },
    hide(state, payload) {
      const currentCategory = state.catagory.find(item => item.id === payload)
      currentCategory.open = false
    }
  },
  //获取数据
  actions: {
    async getCatagory(context) {
      const data = await findAllCatagory()
      data.result.forEach(item => {
        item.open = false
      })
      context.commit('setCatagory', data.result)
    }
  }
}
